package cn.com.zpon.shouyinyuandemo.util;
import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.text.TextUtils;
import android.widget.Toast;

import cn.com.zpon.shouyinyuandemo.PayApplication;

public class ToastUtil {

    private static Handler handler = new Handler(Looper.getMainLooper());

    private static Toast toast = null;

    private static Object synObj = new Object();

    public static void showToast(int resid) {
        showToast(resid, Toast.LENGTH_SHORT);
    }

    public static void showToast(String msg) {
        showToast(msg, Toast.LENGTH_SHORT);
    }

    /**
     * 新增两个showToastAt, 设置显示位置
     *
     * @param msg
     * @param location
     */
    public static void showToastAt(String msg, int location) {
        showToastAt(msg, Toast.LENGTH_SHORT, location);
    }

    public static void showToastAt(int msg, int location) {
        showToastAt(msg, Toast.LENGTH_SHORT, location);
    }

    public static void showToast(final String msg, final int showtime) {
        if (TextUtils.isEmpty(msg)) {
            return;
        }

        handler.post(new Runnable() {
            @Override
            public void run() {
                synchronized (synObj) { //加上同步是为了每个toast只要有机会显示出来
                    if (toast != null) {
                        //toast.cancel();
                        toast.setText(msg);
                        toast.setDuration(showtime);
                    } else {
                        toast = Toast.makeText(PayApplication.getInstance().getApplicationContext(), msg, showtime);
                    }
                    toast.show();
                }
            }
        });
    }

    public static void showToast(final int resid, final int showtime) {
        if (resid == 0) {
            return;
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                synchronized (synObj) { //加上同步是为了每个toast只要有机会显示出来
                    if (toast != null) {
                        //toast.cancel();
                        toast.setText(resid);
                        toast.setDuration(showtime);
                    } else {
                        toast = Toast.makeText(PayApplication.getInstance().getApplicationContext(), resid, showtime);
                    }
                    toast.show();
                }
            }
        });
    }

    public static void showToastAt(final String msg, final int showtime, final int location) {
        if (TextUtils.isEmpty(msg)) {
            return;
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                synchronized (synObj) { //加上同步是为了每个toast只要有机会显示出来
                    if (toast != null) {
                        //toast.cancel();
                        toast.setText(msg);
                        toast.setDuration(showtime);
                    } else {
                        toast = Toast.makeText(PayApplication.getInstance().getApplicationContext(), msg, showtime);
                    }
                    toast.setGravity(location, 0, 0);
                    toast.show();
                }
            }
        });
    }

    public static void showToastAt(final int resid, final int showtime, final int location) {
        if (resid == 0) {
            return;
        }
        handler.post(new Runnable() {
            @Override
            public void run() {
                synchronized (synObj) { //加上同步是为了每个toast只要有机会显示出来
                    if (toast != null) {
                        //toast.cancel();
                        toast.setText(resid);
                        toast.setDuration(showtime);
                    } else {
                        toast = Toast.makeText(PayApplication.getInstance().getApplicationContext(), resid, showtime);
                    }
                    toast.setGravity(location, 0, 0);
                    toast.show();
                }
            }
        });

    }


    /**
     * 避免Toast多次点击后一直提示，消除不了
     *
     * @param context
     * @param content
     */
    public static void showToast(Context context, String content) {
        if (toast == null) {
            toast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
        } else {
            toast.setText(content);
        }
        toast.show();
    }
}